package cn.itcast.controller;

import cn.itcast.domain.User;
import cn.itcast.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


public class Demo1 {
    private SqlSession sqlSession;
    private UserMapper mapper;

    @Before
    public void before() throws Exception{
        //1.读取核心配置文件
        InputStream is = Resources.getResourceAsStream("Sql.xml");

        //2.构建sqlsessionfactory工厂对象
            SqlSessionFactory sessionFactory =new SqlSessionFactoryBuilder().build(is);

        //3..获取sqlsession对象
        sqlSession = sessionFactory.openSession();
        //获取接口的实现类对象
        mapper = sqlSession.getMapper(UserMapper.class);
    }
    @After
    public void After(){
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }

    //查询所有
    @Test
    public void findAll(){
        List<User> userList = mapper.findAll();
        System.out.println(userList);

    }
    //根据id 查询
    @Test
    public void findById(){
        User user = mapper.findById(5);
        System.out.println(user);
    }

    //根据id查询2
    @Test
    public void findByUser(){
        User u = new User();
        u.setId(1);
        List<User> list = mapper.findByUser(u);
        System.out.println(list);

    }
    //多条件查询
    @Test
    public void findConditions(){
        User u = new User();
        /*u.setId(16);
        u.setUsername("张大明");*/
        List<User> conditions = mapper.findConditions(u);
        for (User condition : conditions) {
            System.out.println(condition);
        }
    }

    //list条件查询
    @Test
    public void findByList(){
        List list = new ArrayList();
        list.add(1);
        List<User> users = mapper.findByList(list);
        for (User user : users) {
            System.out.println(user);
        }
    }
    //根据多个id查询
    @Test
    public void findByIds(){
        List list = new ArrayList();
        list.add(25);
        list.add(26);
        list.add(27);
        List<User> users = mapper.findByIds(list);
        System.out.println(users);

    }

    //插入一条数据,获取插入后主键列的值
    @Test
    public void insert(){
        User u = new User();
        u.setUsername("诸葛神力");
        u.setAddress("爱情公寓");
        System.out.println(u);
        mapper.insert(u);
        System.out.println(u);
    }
    //修改一条数据
    @Test
    public void update(){
        User u = new User();
        u.setId(35);

        u.setAddress("上海");
        System.out.println(u);
        mapper.update(u);

    }

    //动态插入一条数据,获取插入后主键列的值
    @Test
    public void insertDynamic(){
        User u = new User();
        u.setUsername("斯内克");

        System.out.println(u);
        mapper.insert(u);
        System.out.println(u);

    }

}
